<?php
/**
 * Embrace Modelling for PHP/JS
 * 
 * Erzeugt aus XML Daten ein PHP Interface, eine Abstrakte PHP Klasse, 
 * eine JS Klasse.
 * 
 * The EMP (Embrace Modelling for PHP/JS) is Modelling Framework for 
 * model driven developement(MDD) in PHP and JS. 
 * 
 * EMP is explicitly not using UML as language for defining models. Because
 * EMP should be easy to understand and to use.
 * 
 * EMP creates from the model following sources:
 * <ul>
 * 		<li>interface - PHP</li>
 * 		<li>abstract class - PHP</li>
 * 		<li>basic implementation class - PHP</li>
 * 		<li>service for connecting to JS - PHP</li>
 * 		<li> async class - JavaScript</li>
 * 	</ul>
 * 
 * All generated sources will be documented with phpDoc/jsDoc standards 
 * for creating API documentations.
 * 
 * Future projects:
 * ====================================
 * 
 * Embrace Composer:
 * ------------------------------------
 * 	Component Modeller:
 *  created components by using models and bind data models from EMP 
 *  to the components.
 *  
 *  Validation:
 *  Components fields could be validated against simple data datatypes
 *  and Sstrings against regular expressions.
 *  
 *  View Modeller:
 *  Components could be bound into component collections(views) for complex
 *  data forms and views.
 * 
 * Embrace Application Kit:
 * ------------------------------------
 *  Authorization Module:
 *  easy to configure login screen to authorize users.
 *  
 *  Applcation Frame:
 *  With three level navigation (Application, Module, Inner-Module). Easy to
 *  use components like tabbed views, sortable tables.
 *  
 * Embrace Messaging Queue:
 * ------------------------------------
 * 	Send messages to the MQ Daemon and use the features for queued processes and
 *  not real-time features like background processes etc.
 *  
 *  Use the EMQ for informing clients with notifications and control client 
 *  features event-like.
 *  
 *  @author s.marletta
 */
require_once("lib/EmbraceSettings.php");
require_once("lib/EmbraceParameters.php");
require_once("lib/EmbraceModel.php");

/**
 * Makes directory, returns TRUE if exists or made
 *
 * @param string $pathname The directory path.
 * @return boolean returns TRUE if exists or made or FALSE on failure.
 */

function mkdirRecursive($pathname, $mode) {
    is_dir(dirname($pathname)) || mkdirRecursive(dirname($pathname), $mode);
    return is_dir($pathname) || @mkdir($pathname, $mode);
}
	
session_start();

$startDir = "models";
$arrModelXml = scandir($startDir);

foreach($arrModelXml as $modelXmlFileName) {
	if(is_file($startDir . "/" . $modelXmlFileName)) {
		$embraceSettings = new EmbraceSettings();
		$embraceParams  = new EmbraceParameters();
		
		$xml = simplexml_load_file($startDir . "/" . $modelXmlFileName);
		?>
		
		<div 
			style="width: 600px; margin: auto; border: 1px solid black; padding: 20px;"
		>
			<div style="font-weight: bold; line-height: 36px; border-bottom: 2px dashed black;">
				Modelname: <?php echo $xml->model->name; ?>
			</div>
		<?php 		
		$embraceSettings->setup($xml);
		$embraceParams->addParameter(
			"Version",
			"".$xml->model->version
		);
		$validate = $embraceSettings->getSetting("validate");
		if($validate !== false) {
			switch($validate) {
				case "md5":
					$embraceParams->addParameter(
						"validate",
						md5_file($startDir . "/" . $modelXmlFileName)
					);
				break;
				
				case "sha":
					$embraceParams->addParameter(
						"validate",
						sha1_file($startDir . "/" . $modelXmlFileName)
					);
				break;
				
				case "svn":
					throw new Exception("Bitte Validierung festlegen.");
				break;
				
				default:
					throw new Exception("Bitte Validierung festlegen.");
				break;
			}
		}else
			throw new Exception("Bitte Validierung festlegen.");
		?>
		<div style="line-height: 30px;">
			XML Settings
		</div>
		<div style="border: 1px solid #CCCCCC; padding: 6px;">
		<table cellspacing="0" cellpadding="0" style="width: 100%;">
		  <tr>
		    <th style="text-align: left; width: 175px;">setting</th>
		    <th style="text-align: left; width: 175px;">value</th>
		  </tr>
		  
		<?php 
			$arrSettings = $embraceSettings->toArray();
			foreach($arrSettings as $key => $param) {
				?>
				<tr>
					<td style="text-align: left; width: 175px;"><?php echo $key; ?></td>
					<td style="text-align: left; width: 175px;"><?php echo $param; ?></td>
				</tr>
				<?php
			}
		?>
		</table>
		</div>
		
		<div style="line-height: 30px;">
			XML Parameters
		</div>
		<div style="border: 1px solid #CCCCCC; padding: 6px;">
		<table cellspacing="0" cellpadding="0" style="width: 100%;">
		  <tr>
		    <th style="text-align: left; width: 175px;">parameter</th>
		    <th style="text-align: left; width: 175px;">value</th>
		  </tr>
		  
		<?php 
			$arrParams = $embraceParams->toArray();
			foreach($arrParams as $key => $param) {
				?>
				<tr>
					<td style="text-align: left; width: 175px;"><?php echo $key; ?></td>
					<td style="text-align: left; width: 175px;"><?php echo $param; ?></td>
				</tr>
				<?php
			}
		?>
		</table>
		</div>
		<?php
		
		/**
		 * Eigentliche Lohik.
		 */
		$empModel = new EmbraceModel("".$xml->model->name, "".$xml->model->version);
		$empModel->setParameters($embraceParams);
		$empModel->setSettings($embraceSettings);
		
		$empModel->transform($xml);
		
		$empModel->createReport();
		?>
		</div>
		<?php
	}
}
?>